package com.qfedu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qfedu.entity.*;
import com.qfedu.service.*;
import com.qfedu.util.Ajaxd;
import com.qfedu.util.Page;
import com.qfedu.util.uuidutil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
public class OrderController {
    @Reference(check = false)
    private RouteService routeService;
    @Reference(check = false)
    private AddressService addressService;
    @Reference(check = false)
    private BoatService boatService;
    @Reference(check = false)
    private BoxService boxService;
    @Reference(check = false)
    private GoodsService goodsService;
    @Reference(check = false)
    private OrderService orderService;

    //商品插入
    @RequestMapping("/insertGoods.do")
    public String doinsertGoods(GoodsEntity goodsEntity,int sid,HttpServletRequest request){
        //商品插入
        Boolean aBoolean = goodsService.insertGoods(goodsEntity);
        //查询当前船的集装箱类型
        List<BoxEntity> boxlist = orderService.selectBoxBySid(sid);
        request.setAttribute("oid",goodsEntity.getOid());
        request.setAttribute("boxlist",boxlist);
        request.setAttribute("bulk",goodsEntity.getGbulk());
        if (aBoolean){
            return "placeorder2";
        }else{
            return "error";
        }
    }

    //订单插入
    @RequestMapping("/orderinsert.do")
    public String doorderinsert(RouteEntity routeEntity, HttpServletRequest request){
        String oid = uuidutil.getuuid();
        HttpSession session = request.getSession();
        UserEntity user = (UserEntity) session.getAttribute("user");
        if (user==null){
            return "login";
        }
        int uid = user.getUid();
        request.setAttribute("oid",oid);
//        request.setAttribute("sid",routeEntity.getSid());

        OrderEntity orderEntity = new OrderEntity(oid, routeEntity.getRid(), 1, uid);
        Date date = new Date();
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        int month=instance.get(Calendar.MONTH);
        int year=instance.get(Calendar.YEAR);
        orderEntity.setOyear(year);
        orderEntity.setOmonth(month);
        orderEntity.setCreateTime(date);
        //查询所有的船只
        List<BoatEntity> boatEntities = boatService.queryByRid(routeEntity.getRid());
        request.setAttribute("boatlist",boatEntities);
        Boolean flag = orderService.insertOrder(orderEntity);
        if (flag){
            return "placeorder";
        }else{
            return "error";
        }
    }

    //订单更新
    @RequestMapping("/updateorderSid.do")
    public String doupdateorder(OrderEntity orderEntity,HttpServletRequest request){
        request.setAttribute("oid",orderEntity.getOid());
        request.setAttribute("sid",orderEntity.getSid());
        Boolean aBoolean = orderService.updateOrder(orderEntity);
        if (aBoolean){
            return "placeorder1";
        }else{
            return "error";
        }
    }


//价格总计
    @RequestMapping("/totailmoney.do")
    @ResponseBody
    public double dototailmoney(double bulk,int bid,String oid){
        BoxEntity boxEntity = boxService.queryBox(bid);
        double bprice = boxEntity.getBprice();
        float allprice= Float.parseFloat(String.valueOf(bulk*bprice));
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOid(oid);
        orderEntity.setOprice(allprice);
        Boolean aBoolean = orderService.updateOrder(orderEntity);
        return bulk*bprice;

    }

    //个人订单查询
    @RequestMapping
    @ResponseBody
    public Ajaxd queryMyOrder(HttpServletRequest request){
        Ajaxd ajaxd = new Ajaxd();
        HttpSession session = request.getSession();
        UserEntity user = (UserEntity)session.getAttribute("user");
        return ajaxd;
    }

    @RequestMapping("/addorderaddress.do")
    public String doaddordeaddress(String oid,HttpServletRequest request){
        request.setAttribute("oid",oid);
        HttpSession session = request.getSession();
        UserEntity user = (UserEntity) session.getAttribute("user");
        AddressEntity addressEntity = new AddressEntity();
        addressEntity.setUid(user.getUid());
        List<AddressEntity> queryaddress = addressService.queryaddress(addressEntity);
        request.setAttribute("address",queryaddress);
        return "myshr";
    }

    @RequestMapping("/uodateorderpho")
    public String topayoff(AddressEntity addressEntity,String oid,HttpServletRequest request){
        if (addressEntity.getFname()==null){
             addressEntity = addressService.queryad(addressEntity.getAid());
        }
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOid(oid);
        orderEntity.setOphone(addressEntity.getSphone());
        Boolean aBoolean = orderService.updateOrder(orderEntity);
        //查订单
        OrderEntity queryone = orderService.queryone(oid);
        //查询商品
        List<GoodsEntity> goodsEntities = goodsService.queryGoodsBYOid(oid);
        //查航线
        RouteEntity routeEntity = routeService.queryRouteById(queryone.getRid());
        String format = dataformat(routeEntity.getTime());
        routeEntity.setTimes(format);
        queryone.setRouteEntity(routeEntity);
        if (aBoolean){
            request.setAttribute("order",queryone);
            request.setAttribute("goodslist",goodsEntities);
            return "payoff";
        }else{
            return "error";
        }
    }

    @RequestMapping("/payoff.do")
    @ResponseBody
    public Ajaxd dopayoff(OrderEntity orderEntity){
        Ajaxd ajaxd = new Ajaxd();
        orderEntity.setOstatus(2);
        Boolean aBoolean = orderService.updateOrder(orderEntity);
        if (aBoolean){
            ajaxd.setFlag(true);
        }else {
            ajaxd.setFlag(false);
        }
        return ajaxd;
    }

    @RequestMapping("/queryAllOrder.to")
    public String doqueryMyOrder(){
        return "myOrder";
    }

    @RequestMapping("/queryMyOrder.do")
    @ResponseBody
    public Ajaxd doqueryPageOrder(int pageNum, @RequestParam(value = "pageSize",required = false,defaultValue = "2") int pageSize,String tj,HttpServletRequest request){
        Ajaxd ajaxd = new Ajaxd();
        HttpSession session = request.getSession();
        UserEntity user = (UserEntity)session.getAttribute("user");
        Page page = orderService.queryByPage(pageNum, pageSize,user.getUid(),tj,0);
        List<OrderEntity> orderlist = page.getOrderlist();
        for (OrderEntity orderEntity:orderlist){
            RouteEntity routeEntity = routeService.queryRouteById(orderEntity.getRid());
            String dataformat = dataformat(routeEntity.getTime());
            routeEntity.setTimes(dataformat);
            orderEntity.setRouteEntity(routeEntity);
        }
        page.setOrderlist(orderlist);
        if (page!=null){
            ajaxd.setPage(page);
            ajaxd.setFlag(true);
        }else {
            ajaxd.setFlag(false);
        }
        return ajaxd;
    }

    private String dataformat(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = simpleDateFormat.format(date);
        return format;
    }


    @RequestMapping("/orderdetail.do")
    public String doorderdetail(String oid,HttpServletRequest request){
//查订单
        OrderEntity queryone = orderService.queryone(oid);
        //查询商品
        List<GoodsEntity> goodsEntities = goodsService.queryGoodsBYOid(oid);
        //查航线
        RouteEntity routeEntity = routeService.queryRouteById(queryone.getRid());
        String format = dataformat(routeEntity.getTime());
        routeEntity.setTimes(format);
        queryone.setRouteEntity(routeEntity);
        request.setAttribute("goodslist",goodsEntities);
        request.setAttribute("order",queryone);
        return "orderdetail";
    }
}

